/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package master.member;

import common.util.HibernateUtil;
import java.awt.HeadlessException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import master.member.object.MemberHistory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import system.SystemMassages;
import template.DefaultRegistrationForm;
import template.RegistrantionPanel;
import template.SERDefaultRegistrationForm;

/**
 *
 * @author Rabid
 */
public class REGMember extends DefaultRegistrationForm{
    @Override
    public RegistrantionPanel objectCreator() {
        member = new PCMember();
        return member;
    }

    @Override
    public Object initObject(RegistrantionPanel panel) {
        return panel.initObject();
    }

    @Override
    protected String setFrameTitle() {
        return TITLE;
    }

//    @Override
//    protected List getDisplayComponents(RegistrantionPanel panel) {
//        return panel.getDisplayComponents();
//    }
//    
    @Override
    protected String getViewDataList() {
        return "from master.member.object.Member";
    }
    
    public static final String TITLE = "Member Registration";

    @Override
    protected void getAutoFillComponents(RegistrantionPanel panel) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
        @Override
    protected void save() {

        Object[] initObject = member.initObjectList();
        System.gc();
        Session session = HibernateUtil.getSession();
        Transaction transaction = session.beginTransaction();
        try {
            for (Object object : initObject) {
                if (!((MemberHistory) object).getMember().isSaved()) {
                    session.save(object);
                } else {
                    session.update(((MemberHistory)object).getMember());
                }
            }
            transaction.commit();
            member.resetAll();
            JOptionPane.showMessageDialog(null, SystemMassages.SAVED_SUCCESS);
        } catch (HibernateException | HeadlessException e) {
            transaction.rollback();
            JOptionPane.showMessageDialog(null, SystemMassages.SAVING_FAIL);
            Logger.getLogger(SERDefaultRegistrationForm.class.getName()).log(Level.SEVERE, null, e);
        } finally {
            HibernateUtil.closeSession();
//            SystemVariyable.session.close();
        }
    }
    private PCMember member;
}
